import time

import matplotlib.pylab as plt

import alg
import generator as g

data_list = {"insert": [], "bubble": [], "quick": [], "cock_tail_sort": [], "heap_sort": [], "counting_sort": [],
             "shell_sort": [], "merge_sort": []}
x = []

def run(alg_func, data):
    start = time.time()
    alg_func(data)
    end = time.time()
    data_list[alg_func.__name__].append(end - start)
    print(f'{alg_func.__name__} running in {end - start}')
    return end - start


for i in range(1000, 11000, 1000):
    # data = g.get_shuffle_data(num=i)
    # data = g.get_positive_data(num=i)
    data = g.get_reversed_data(num=i)
    x.append(i)
    run(alg.insert, data)
    run(alg.quick, data)
    run(alg.bubble, data)
    run(alg.cock_tail_sort, data)
    run(alg.heap_sort, data)
    run(alg.counting_sort, data)
    run(alg.shell_sort, data)
    run(alg.merge_sort, data)

# 打印二维列表，查看运行时间数据

print(data_list)

# 画图
plt.plot(x, data_list["insert"], label="Insertion Sort")
plt.plot(x, data_list["bubble"], label="Bubble Sort")
plt.plot(x, data_list["quick"], label="Quick Sort")
plt.plot(x, data_list["cock_tail_sort"], label="Cocktail Sort")
plt.plot(x, data_list["heap_sort"], label="Heap Sort")
plt.plot(x, data_list["counting_sort"], label="Counting Sort")
plt.plot(x, data_list["shell_sort"], label="Shell Sort")
plt.plot(x, data_list["merge_sort"], label="Merge Sort")

# plt.xlabel("Shuffle Data Set Size")
# plt.xlabel("positive Data Set Size")
plt.xlabel("reversed Data Set Size")
plt.ylabel("Time (in seconds)")
plt.title("Sorting Algorithm Comparison")

plt.legend()
plt.show()

# 太过接近，单独画图
plt.figure()
plt.plot(x, data_list["quick"], label="Quick Sort")
plt.plot(x, data_list["cock_tail_sort"], label="Cocktail Sort")
plt.plot(x, data_list["heap_sort"], label="Heap Sort")
plt.plot(x, data_list["counting_sort"], label="Counting Sort")
plt.plot(x, data_list["shell_sort"], label="Shell Sort")
plt.plot(x, data_list["merge_sort"], label="Merge Sort")

# plt.xlabel("Shuffle Data Set Size")
# plt.xlabel("positive Data Set Size")
plt.xlabel("reversed Data Set Size")
plt.ylabel("Time (in seconds)")
plt.title("Sorting Algorithm Comparison")

plt.legend()
plt.show()
